package cn.tangsihai.pet.controller;

import cn.tangsihai.basic.exception.MyException;
import cn.tangsihai.basic.util.AjaxResult;
import cn.tangsihai.basic.util.PageList;
import cn.tangsihai.pet.domain.Pet;
import cn.tangsihai.pet.query.PetQuery;
import cn.tangsihai.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    public AjaxResult pageList(@RequestBody PetQuery query){
        try {
            PageList<Pet> petPageList = petService.queryList(query);
            return AjaxResult.me().setResultObj(petPageList);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }

    /**
     * 上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return AjaxResult.me();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }

    /**
     * 下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AjaxResult.me();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }

    /**
     * 门户界面分页查询，查询状态为上架的宠物信息
     * @param query
     * @return
     */
    @PostMapping("/pageListOnForm")
    public AjaxResult pageListOnForm(@RequestBody PetQuery query){
        try {
            query.setState(Pet.ONSALE_IN_PET);
            PageList<Pet> petPageList = petService.queryList(query);
            return AjaxResult.me().setResultObj(petPageList);
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }


    /**
     * 根据id查询上架的宠物信息
     * @param id
     * @return
     */
    @GetMapping("/getDetail/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id){
        try {
            // 注：一定是查询上架的数据信息
            return AjaxResult.me().setResultObj(petService.findByIdAndState(id, Pet.ONSALE_IN_PET));
        }catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }
}
